﻿var geocoder;
var map;
var myOptions;
var allData = new Array();

$(document).ready(function () {
    InitializeMap();
});

function InitializeMap() {
    geocoder = new google.maps.Geocoder();
    myOptions = {
        center: new google.maps.LatLng(34.03, -118.14), //default location
        zoom: 11,
        disableDefaultUI: false, //By default its false, true if you don't want default UI Setting of Map
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("mapContainer"), myOptions);
    GetMapData();
}

function GetMapData() {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "google-map.aspx/GetAllLocation",
        async: true,
        cache: true,
        dataType: "json",
        success: function (data) {
            allData = data.d;
            LoadAddress();
        }
    });
}

function LoadAddress() {
    var dataForInfoWin = new Array();
    if (allData != null) {
        for (i = 0; i < allData.length; i++) {
            var addLatLong = new google.maps.LatLng(allData[i].Latitude, allData[i].Longitude);
            map.setCenter(addLatLong);

            dataForInfoWin[i] = "<div><p>" + allData[i].Name + "</p></div>";

            var infowindow = new google.maps.InfoWindow();
            var image = new google.maps.MarkerImage(BaseUrl + GetMarkerIcon(),
                new google.maps.Size(100, 100),
                new google.maps.Point(0, 0),
                new google.maps.Point(25, 75));

            marker = new google.maps.Marker({
                map: map,
                position: addLatLong,
                icon: image
            });

            google.maps.event.addListener(marker, 'mouseover', (function (marker, i) {
                return function () {
                    infowindow.setContent(dataForInfoWin[i]);
                    infowindow.open(map, marker);
                }
            })(marker, i));

            google.maps.event.addListener(marker, 'click', (function (marker, i) {
                return function () {
                    infowindow.setContent(dataForInfoWin[i]);
                    infowindow.open(map, marker);
                    SetFacilityOnGoogleMapMarkerClick(allData[i].FacilityId);
                }
            })(marker, i));

            google.maps.event.addListener(marker, 'mouseout', (function (marker, i) {
                return function () {
                    infowindow.close();
                }
            })(marker, i));
        }

    }
}

function GetMarkerIcon() {
    return "Google-Map-Example/images/marker-ico.png";
}
